package component{

        import flash.events.*;
        import flash.net.*;
        
        import mx.containers.*;
        import mx.controls.*;
        import mx.events.*;
        import mx.managers.*;
        
        import org.pubflex.ModelUtility;
        import org.pubflex.SocketURLLoader;

    public class NewEntryBase extends ViewStack{
        namespace ATOM = "http://www.w3.org/2005/Atom";
  
  
        public static const POST_COMPLETE:String = "postComplete";
          
        [Bindable] public var publishButton:Label;
        [Bindable] public var baseURI:String;
        [Bindable] public var urlLoader:SocketURLLoader = new SocketURLLoader();
        
        [Bindable] public var entryTitle:TextArea;
        [Bindable] public var entryContent:TextArea;
        

        public var collectionsComboBox:ComboBox;
        
        public function NewEntryBase(){
            super();
        }        

             	     
        public function closeWindow(e:Event):void {
            PopUpManager.removePopUp(this); 
        }           
        
        private function publish(e:Event):void {
            PopUpManager.removePopUp(this); 
        } 
        
        public function comboChanged(e:Event):void{
            trace(e.target);
            trace(e.currentTarget);
        }
        
        public function handlePOST(e:Event):void{
            namespace ATOM = "http://www.w3.org/2005/Atom"
            namespace APP = "http://www.w3.org/2007/app";
            use namespace APP;   
            use namespace ATOM;
            
            trace(e);
            trace(e.currentTarget);
            trace("current target data : " + e.currentTarget.data);            
            trace(collectionsComboBox);
            trace("Selected Item : " + collectionsComboBox.selectedItem);            
            trace(collectionsComboBox.selectedItem.data.@href);                        
            trace(collectionsComboBox.dataProvider);
            trace(collectionsComboBox.selectedIndex);
            
            trace( collectionsComboBox.getChildAt(  collectionsComboBox.selectedIndex ));
            
            var deleteButton:LinkButton = e.currentTarget as LinkButton; 
            var xml:XML = collectionsComboBox.selectedItem.data as XML;
            trace("Collection XML " + xml);
            var path:String = xml.@href;
            trace ( xml.@base ) ;
            
           var request:URLRequest = new URLRequest();
            request.url = this.baseURI + path;
            trace(request.url);

            //var authHeader:URLRequestHeader = new URLRequestHeader("Authorization","Basic " + your_credentials);
            //request.requestHeaders.push(authHeader);
            //Security.loadPolicyFile('"http://yourhost.com/crossdomain.xml');
            urlLoader.showBusyCursor = true;
            urlLoader.requestTimeout = 10000;
            urlLoader.method = "POST"

            var x:XML = ModelUtility.newEntry(this);
  
           request.data = x;

            urlLoader.addEventListener(Event.COMPLETE, handleCompletionEvent);
			urlLoader.addEventListener(HTTPStatusEvent.HTTP_STATUS, handleStatusEvent);
			urlLoader.addEventListener(IOErrorEvent.IO_ERROR, handleError);
			//loader.addEventListener(ProgressEvent.PROGRESS, traceEvent);
			//urlLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, traceEvent);
   		    		 
            urlLoader.load(request);
            this.parent.removeChild(this);
            dispatchEvent(new Event(NewEntryBase.POST_COMPLETE,true,false));
            
        }
        
        public function handleError(e:Event):void{
            trace(e);
        }
    
        public function clearForm():void{
            //authorList.dataProvider = null;
            //entryContent.text = null; 
            //entrySummary.text = null;

        }
        public function handleCompletionEvent(e:Event):void{
            trace("handleCompletionEvent() called")
            trace(e);
            trace("Reponse Headers : " + urlLoader.responseHeaders);
            trace("Propagating event");
            clearForm();
            dispatchEvent(e);            
        }
    
        private function handleStatusEvent(event:HTTPStatusEvent, token:Object=null):void {
            trace("Event Status : " + event.status);  
            trace("Token : " + token);  
            var status:int = event.status;                       
            trace(event.responseHeaders);          
            dispatchEvent(event);
        }      
    }
}